var express = require('express');
var router = express.Router();
var msg = require('../utils/message')
var fun = require("../controller/user")
var jwt = require('../config/token')

/* GET users listing. */
router.get('/', function (req, res, next) {
  jwt.checkToken(req.headers.authorization).then(data => {
    res.json(msg(200, [], '用户列表访问成功'))
  }).catch(err => {
    res.json(msg(400, [], '用户状态错误'))
  })

});
//登录
router.get('/login', function (req, res, next) {
  let reqData = req.query;
  fun.login(reqData).then(data => {
    let response = data[0];
    if (data.length == 1 && reqData.password == response.password) {
      response.token = jwt.createToken({ id: response.id });
      res.json(msg(200, ...data, '登录成功'))
    } else if (data.length == 1 && reqData.password != response.password) {
      res.json(msg(500, [], '账号或密码错误'))
    } else {
      res.json(msg(500, [], '账号不存在'))
    }
  });
});

//注册
router.get('/insert', function (req, res, next) {
  res.json({ title: "ihao" })
});

module.exports = router;
